// pages/bindPhoneNum/bindPhoneNum.js
var app = getApp();
var timer;
const pushCodeUri = "/EnvePacket/pushCode";
const bindPhoneUrl='/EnvePacket/bindPhone';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading:1,//是否显示加载中样式
    isWithdrawalsSuccessShow:0,//控制绑定手机号成功弹层的显示
    winWidth:'',//页面宽度
    winHeight:'',//页面高度
    old_uid: '',//旧用户id，判断是否继承过以前就关系
    isBindPhoneNum:0,//判断用户是否绑定过手机号
    time:60,//获取验证码后需要等待的时间
    phone:'',//用户手机号
    code:'',//验证码
    btnText: "获取验证码",//验证码发送
    btnDisplay: false,//发送验证码按钮是否禁用
  },
  goBack:function(e){
    var formId = e.detail.formId;
    app.postFormId(this.data.token, formId);
    wx.navigateBack({
      delta: 1
    })
  },
  // 关闭绑定成功弹层
  withdrawalsCloseBtn: function () {
    var _this = this;
    _this.setData({
      isWithdrawalsSuccessShow: 0
    })
  },
  //输入手机号
  phoneInputEvent: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //输入验证码
  codeInputEvent: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 确认绑定手机号
  bindPhone:function(e){
    var that = this;
    var formId = e.detail.formId;
    app.postFormId(that.data.token, formId);
    if (that.data.code == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: "none",
        duration: 2000
      });
      return false;
    }
    var requestData = {
      token: that.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      phone: that.data.phone,
      code:that.data.code
    }
    app.post(bindPhoneUrl, requestData, (res) => {
      if(res.code==2000){
        that.setData({
          isWithdrawalsSuccessShow:1,
          isBindPhoneNum:1,
          old_uid: res.data.old_uid
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'loading',
          duration: 2000
        })
      }
    });
  },
  /*发送验证码*/
  pushCodeEvent: function (e) {
    var that = this;
    // var formId = e.detail.formId;
    // app.postFormId(that.data.token, formId);
    if (that.data.phone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: "none",
        duration: 2000
      });
    } else {
      //禁用按钮
      that.setData({
        btnDisplay: true
      });
      //启动定时器
      if (timer) {
        clearInterval(timer);
      }

      var requestData = {
        token: that.data.token,
        version: app.setConfig.version,
        type: app.setConfig.type,
        phone: that.data.phone
      }
      app.post(pushCodeUri, requestData, (res) => {
        if (res.code == 2000) {
          wx.showToast({
            title: '验证码发送成功',
            icon: "none",
            duration: 2000
          });
          timer = setInterval(() => {
            var timeNow = that.data.time
            if (timeNow > 0) {
              that.setData({
                time: timeNow - 1,
                btnText: '(' + (timeNow - 1) + "s) 重新获取"
              })
            } else {
              clearInterval(timer);
              that.setData({
                time: 60,
                btnText: "重新获取",
                btnDisplay: false
              })
            }
          }, 1000)
        } else {
          clearInterval(timer);
          that.setData({
            time: 60,
            btnText: "获取验证码",
            btnDisplay: false
          })
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000
          });
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var isBindPhoneNum;
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        })
      }
    });
    if (options.phone==''){
      isBindPhoneNum=0;
    }else{
      isBindPhoneNum = 1;
    }
    _this.setData({
      isLoading:0,
      old_uid: options.old_uid,
      phone: options.phone,
      isBindPhoneNum: isBindPhoneNum
    })
    // console.log(options);
    _this.loop();
  },
  // 判断是否获取到token的值了
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    var theSysStatus = wx.getStorageSync('sysStatus');
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken,
        sysStatus: theSysStatus,
        pageFile: 1
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})